using UnityEngine;
using System.Collections;

/// <summary>
/// Usa el WeaponId para saber que tipo de arma es, usa el el template para saber las visuales y el comportamiento.
/// </summary>
public class Weapon : Item
{
    public string m_WeaponId = "";
    WeaponBehaviour m_Behaviour = null;
    public MyGUIText m_BulletText;

    string m_ReqPerk = "";

    bool m_Initialized = false;
	void Start () 
    {
        if (m_Initialized == false)
        {
            Initialize();
        }
	}
    public void Initialize()
    {
        this.textGUI = m_BulletText.gameObject;
        m_Behaviour = WeaponManager.GetInstance().Create(m_WeaponId, false);
        if (m_Behaviour == null)
        {
            Debug.LogError("Weapon id is null");
        }
        else
        {
            //Uso el template para saber cuando espacio ocupa y instancio las visuales de mundo y de GUI del arma.
            spaceMatrix = m_Behaviour.m_Template.inventorySpace;
            visualModelGUI = GameObject.Instantiate(m_Behaviour.m_Template.visualModelGUI) as GameObject;
            visualModelWorld = GameObject.Instantiate(m_Behaviour.m_Template.visualModelWorld) as GameObject;

            visualModelGUI.transform.parent = this.transform;
            visualModelWorld.transform.parent = this.transform;
            visualModelGUI.transform.localPosition = Vector3.zero;
            visualModelWorld.transform.localPosition = Vector3.zero;
            visualModelGUI.SetActive(false);

            m_ReqPerk = m_Behaviour.m_Template.RequieredPerk;

            m_BulletText.transform.parent = visualModelGUI.transform;
            m_BulletText.transform.localPosition = new Vector3(-0.2f, -0.3f, 0.0f);
			
			base.description = m_Behaviour.m_Template.ToolTip;
        }
        m_Initialized = true;
    }
    public override void Use()
    {
        if (m_ReqPerk == "")
        {
            Player.instance.Equip(m_Behaviour);
            GUIPanelInGame.instance.OnWeaponEquip(visualModelGUI, Player.instance.GetSelectedCharacter().m_CharacterIndex,this);
        }
        else if((Player.instance.GetSelectedCharacter().m_CharacterData.perks.IsEquipped(m_ReqPerk)))
        {
            Player.instance.Equip(m_Behaviour);
            GUIPanelInGame.instance.OnWeaponEquip(visualModelGUI, Player.instance.GetSelectedCharacter().m_CharacterIndex,this);
        }
        else
        {
            Debug.Log("No tenes el perk para equipar el arma");
        }
    }
    public override void OnDrop()
    {
        //Debug.LogError("blabla");
        base.OnDrop();
        Player.instance.GetSelectedCharacter().UnEquip(m_Behaviour);
    }
    public override void Update()
    {
        base.Update();
        if (InventoryController.GetInstance().IsOpened())
        {
            m_BulletText.SetText(m_Behaviour.m_loadedAmmo.ToString()+" ");
        }
    }
    public int GetRemainingBullets()
    {
        return m_Behaviour.m_loadedAmmo;
    }
}
